<?php
/**
 * 节点管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-07-24
 */
 namespace Admin\Controller;
 use \Org\Com\Tool;
 
class NodeController extends CommonController{
    /*
     * 构造函数
     *
     * @return  #
     */
    public function _initialize(){       
        parent::_initialize();
        $this->db   =new \Model\NodeModel();       
    }
    
    /*
     * 节点列表
     * 
     * @return #
     */
    public function index(){        
        if(Tool::request("p_id")){
            $map['p_id']        =Tool::request("p_id");
        }
        if(I("request.node_name")){
            $map['node_name']   =I("request.node_name","",'trim');
        }
        $map=$this->_search("Node");      
        if(Tool::request("add_time")){
            $add_time           =Tool::strTime(Tool::request("add_time"));
            $map['add_time']    =array("egt",$add_time);
        }         
        /*顶级节点列表 start*/
         $map1=array(
            "state" =>1,
            "level" =>0
        );
        
        $topNoList      =$this->db->where($map1)->order("ord asc")->select();
        $map['state']   =1;
        $sortBy         ='path';
        $sort           =desc;
        $nodeList       =$this->_list($this->db, $map,$sortBy,$sort);
        $this->assign("nodeList",$nodeList);
        $this->assign("topNoList",$topNoList);
        $this->display();
    }
    
    /*
     * 编辑节点，包括添加和删除
     * 
     * @return #
     */
    public function show(){
        $map['level']   =0;
        $map['state']   =1;
        $pNodeList  =$this->_list($this->db,$map,"ord",TRUE);
        $this->assign("pNodeList",$pNodeList);
        if(!I("request.id","","int")){
            $this->display();
        }else{
            $nodeInfo   =$this->db->find(I("request.id","","int")); 
            $this->assign("nodeInfo",$nodeInfo);
            //查询父节点列表            
            $this->display();
        }
    }
    
    /*
     * ajax查询控制器名称并返回控制器
     * 
     * @return array $controlName
     */
    public function ajaxControl(){
        $ajaxP_id   =I("request.pid","","int");
        $controlName=$this->db->find($ajaxP_id);
        $controll   =$controlName['controller'];
        echo $controll;
    }
    
    /*
     * 编辑节点 修改和新增
     * 
     * @return  #
     */
    public function edit(){
        if(Tool::request("id","int")){
            $map['id']          =Tool::request("id","int");
        }
        if(!I("request.node_name",'',"trim")){
            $this->error("节点名不能为空");
        }else{
            $map['node_name']   =I("request.node_name",'',"trim");
        }        
        /*
        if(I("request.node_name",'',"trim")){
            $uniqNode_name      =Tool::fieldUnique("node_name",I("request.node_name",'',"trim"),"node",Tool::request("id","int"));
            if($uniqNode_name){
                $this->error("节点已存在");
            }else{
                $map['node_name']   =I("request.node_name",'',"trim");
            }            
        }else{
            $this->error("节点名不能为空");
        }
         */
        //判断是否为为顶级节点，是顶级节点,level为0，路径为其id，action为空
        if(!(I("post.p_id"))){
            $map['p_id']    =0;
            $map['path']    =I("post.id").'-';
            $map['level']   =0;
        }else{
          //如果不为顶级节点，level为1，获取器父id，id拼接路径用  
            $map['p_id']    =I("post.p_id");
            $map['path']    =I("post.p_id").'-'.I("post.id");
            $map['level']   =1;
        }
        if(I("post.controller")){
            $uniqController =Tool::fieldUnique("controller",ucfirst(strtolower(trim(I("post.controller",'',"trim")))),"node",Tool::request("id","int"));
            if($uniqController){
                $this->error("控制器已存在");
            }else{
                //控制器首字母大写
                $map['controller']=ucfirst(strtolower(trim(I("post.controller",'',"trim"))));
            }
        }
        
        if(I("post.action")){
            $map['action']  =I("post.action");
        }
        if(I("post.ord")){
            $map['ord']     =I("post.ord");
        }
        $map['use_level']   =I("post.use_level","","int");
        $edits              =$this->db->editNode($map);
        if($edits){
            $this->success("节点编辑成功");
        }else{
            $this->error("节点编辑失败");
        }        
    }
    
    /*
     * 删除指定节点
     * 
     * @param   $id 被删除的节点id
     * 
     * @return  boole
     */
    public function del(){
        $map=array(
            'id'        =>Tool::request("id","int"),
            'state'     =>0
        );
        $map2=array(
            'is_show'   =>1,
            'p_id'      =>Tool::request("id","int")
        );
        $node_list      =$this->db->where($map2)->find();
        if($node_list){
            $this->error("存在子节点，不允许删除");            
        }else{
            if($this->db->del($map)){
                $this->success("删除成功");
            }else{
                $this->error("删除失败");
            }
        }
    }        
}